package com.sanpu.kxe.business.repay.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.repay.bean.QueryPlanDetailsOutput;
import com.sanpu.kxe.business.repay.contract.RepayConfig;
import com.sanpu.kxe.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：查询计划明细adapter
 * Created by 薛勇军 on 2019/4/10.
 */

public class QueryPlanDetailsAdapter extends BaseAdapter {

    private Context mContext;

    private boolean isOptionalMcc;

    private List<QueryPlanDetailsOutput.QueryPlanDetails> datas = new ArrayList<>();

    public QueryPlanDetailsAdapter(Context mContext, List<QueryPlanDetailsOutput.QueryPlanDetails> datas) {
        this.mContext = mContext;
        this.datas = datas;
    }

    public void setOptionalMcc(boolean isOptionalMcc) {
        this.isOptionalMcc = isOptionalMcc;
    }

    public void setNotifyDataChanged(List<QueryPlanDetailsOutput.QueryPlanDetails> data) {
        if (datas == null)
            datas = new ArrayList<>();
        datas.clear();
        if (data != null)
            datas.addAll(data);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (datas != null)
            return datas.size();
        return 0;
    }

    @Override
    public QueryPlanDetailsOutput.QueryPlanDetails getItem(int position) {
        if (datas != null && datas.size()>position)
            return datas.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_query_plan_details, null);
        }
        RelativeLayout headerView = ButterKnife.findById(convertView, R.id.headerView);
        TextView typeTv = ButterKnife.findById(convertView, R.id.typeTv);
        TextView dateTv = ButterKnife.findById(convertView, R.id.dateTv);
        TextView businessTv = ButterKnife.findById(convertView, R.id.businessTv);
        TextView businessTitleTv = ButterKnife.findById(convertView, R.id.businessTitleTv);
        TextView moneyTv = ButterKnife.findById(convertView, R.id.moneyTv);
        TextView tradeFeeTv = ButterKnife.findById(convertView, R.id.tradeFeeTv);
        TextView statusTv = ButterKnife.findById(convertView, R.id.statusTv);

        QueryPlanDetailsOutput.QueryPlanDetails retDatBean = getItem(position);
        if (retDatBean != null) {
            if (TextUtils.equals(RepayConfig.TRADETYPE_EXPENSE, retDatBean.getRepayTradeType())) {
                typeTv.setText("消费");
                headerView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_de2149));
            } else if (TextUtils.equals(RepayConfig.TRADETYPE_PAYMENT, retDatBean.getRepayTradeType())) {
                typeTv.setText("还款");
                headerView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.c_2189de));
            }
            String tradeTime = retDatBean.getExpectedTradeTime();
            if (!TextUtils.isEmpty(tradeTime) && tradeTime.length()>4) {
                dateTv.setText(DateUtils.dateToString(tradeTime, "yyyy-MM-dd HH:mm:ss"));
            }
            if (isOptionalMcc && TextUtils.equals(RepayConfig.TRADETYPE_EXPENSE, retDatBean.getRepayTradeType())) {
                businessTv.setVisibility(View.VISIBLE);
                businessTitleTv.setVisibility(View.VISIBLE);
                businessTv.setText(retDatBean.getMccName());
            } else {
                businessTv.setVisibility(View.GONE);
                businessTitleTv.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(retDatBean.getTradeAmount())) {
                moneyTv.setText(retDatBean.getTradeAmount());
            }
            if (!TextUtils.isEmpty(retDatBean.getTradeFee())) {
                tradeFeeTv.setText(retDatBean.getTradeFee());
            }
            if (!TextUtils.isEmpty(retDatBean.getPlanDetailStatus())) {
                statusTv.setText(getPlanByStatus(retDatBean.getPlanDetailStatus()));
            }
        }
        return convertView;
    }

    private String getPlanByStatus(String planStatus) {
        switch (planStatus) {
            case "INIT":
                return "初始化";
            case "WATING":
                return "待执行";
            case "PROCESING":
                return "执行中";
            case "CANCEL":
                return "已取消";
            case "SUCCESS":
                return "执行成功";
            case "FAIL":
                return "执行失败";
            case "COMPENSATING":
                return "补偿中";
            case "COMPENSATE_SUCCESS":
                return "补偿成功";
            case "COMPENSATE_FAIL":
                return "补偿失败";
            default:
                return "";
        }
    }
}
